package com.abso.sunlight.explorer.dialogs;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TrayDialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.abso.sunlight.api.Gender;
import com.abso.sunlight.api.LegislatorQuery;
import com.abso.sunlight.api.Party;
import com.abso.sunlight.api.State;
import com.abso.sunlight.api.SunlightException;
import com.abso.sunlight.api.Title;
import com.abso.sunlight.explorer.CongressExplorerPlugin;
import com.abso.sunlight.explorer.ControlUtils;
import com.abso.sunlight.explorer.LegislatorSearch;
import com.abso.sunlight.explorer.SearchUtils;
import com.abso.sunlight.explorer.SunlightImages;

/**
 * The dialog used to search for legislators.
 */
public class LegislatorSearchDialog extends TrayDialog {

    /** The search to edit. */
    private LegislatorSearch searchToEdit;

    /** The text field displaying the search name. */
    private Text searchNameText;

    /** The list of checkboxes for title criteria. */
    private List<Button> titleCheckBoxes;

    /** The list of checkboxes for party criteria. */
    private List<Button> partyCheckBoxes;

    /** The list of checkboxes for gender criteria. */
    private List<Button> genderCheckBoxes;

    /** The list of checkboxes for in-office criteria. */
    private List<Button> inOfficeCheckBoxes;

    /** The current query configuration. */
    private LegislatorQuery query;

    /** The current name for the search. */
    private String searchName;

    /** The text field for first name criteria. */
    private Text firstNameText;

    /** The text field for middle name criteria. */
    private Text middleNameText;

    /** The text field for last name criteria. */
    private Text lastNameText;

    /** The text field for name suffix criteria. */
    private Text nameSuffixText;

    /** The text field for nickname criteria. */
    private Text nicknameText;

    /** The table for preselecting states. */
    private CheckboxTableViewer stateTableViewer;

    /** The text field for district number criteria. */
    private Text districtNumberText;

    /** The text field for Bioguide ID criteria. */
    private Text bioguideText;

    /** The text field for Project Vote Smart ID criteria. */
    private Text voteSmartText;

    /** The text field for FEC ID criteria. */
    private Text fecText;

    /** The text field for GovTrack.us ID criteria. */
    private Text govTrackText;

    /** The text field for CRP ID criteria. */
    private Text crpText;

    /** The text field for Eventful.com ID criteria. */
    private Text eventfulText;

    /** The text field for Twitter ID criteria. */
    private Text twitterText;

    /** The button to apply changes to a previously saved query. */
    private Button applyButton;

    /**
     * Constructs a new dialog.
     * 
     * @param parentShell
     *            the parent shell.
     * @param searchToEdit
     *            the legislator's search to edit (null for a new search).
     */
    public LegislatorSearchDialog(Shell parentShell, LegislatorSearch searchToEdit) {
        super(parentShell);
        setShellStyle(getShellStyle() | SWT.RESIZE);
        this.searchToEdit = searchToEdit;
    }

    @Override
    protected void configureShell(Shell newShell) {
        super.configureShell(newShell);
        newShell.setText("Search Legislators");
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
     */
    @Override
    protected Control createDialogArea(Composite parent) {
        Control dialogArea = super.createDialogArea(parent);

        /* adds the main composite */
        Composite mainArea = new Composite((Composite) dialogArea, SWT.NONE);
        mainArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        mainArea.setLayout(new GridLayout(2, false));

        /* adds the name field */
        searchNameText = createTextField("Name:", mainArea);
        if (searchToEdit != null) {
            searchNameText.setText(searchToEdit.getName());
            searchNameText.setEditable(false);
        } else {
            searchNameText.setText(CongressExplorerPlugin.getSearchManager().getNextSearchName());
        }
        searchNameText.selectAll();

        /* adds the option tabs */
        CTabFolder tabFolder = new CTabFolder(mainArea, SWT.BORDER);
        GridData tabFolderLayoutData = new GridData(SWT.FILL, SWT.FILL, true, true);
        tabFolderLayoutData.horizontalSpan = 2;
        tabFolder.setLayoutData(tabFolderLayoutData);
        tabFolder.setSimple(false);
        createMainTabItem(tabFolder);
        createLocationTabItem(tabFolder);
        createContactTabItem(tabFolder);

        /* adds the apply button area (if required) */
        if (searchToEdit != null) {
            Composite applyArea = new Composite(mainArea, SWT.NONE);
            GridData applyLayoutData = new GridData(SWT.FILL, SWT.FILL, true, false);
            applyLayoutData.horizontalSpan = 2;
            applyArea.setLayoutData(applyLayoutData);
            GridLayout applyLayout = new GridLayout(1, true);
            applyLayout.marginWidth = 0;
            applyLayout.marginHeight = 0;
            applyLayout.horizontalSpacing = 0;
            applyLayout.verticalSpacing = 0;
            applyArea.setLayout(applyLayout);
            applyButton = new Button(applyArea, SWT.PUSH);
            applyButton.setText("Apply");
            GridData data = new GridData(SWT.END, SWT.END, true, false);
            int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
            Point minSize = applyButton.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
            data.widthHint = Math.max(widthHint, minSize.x);
            applyButton.setLayoutData(data);
            applyButton.addSelectionListener(new SelectionAdapter() {
                @Override
                public void widgetSelected(SelectionEvent e) {
                    updateQuery(/* searchToEdit.getQuery() */);
                }
            });
        }
        applyDialogFont(dialogArea);
        tabFolder.pack();
        tabFolder.setSelection(0);
        return dialogArea;
    }

    /**
     * Creates the tab item with the main search controls (name, party, etc.).
     * 
     * @param tabFolder
     *            the tab folder.
     */
    private void createMainTabItem(CTabFolder tabFolder) {
        CTabItem item = new CTabItem(tabFolder, SWT.NONE);
        item.setText("Main");
        item.setImage(SunlightImages.GENERAL.get());

        /* adds the main composite */
        Composite mainArea = new Composite(tabFolder, SWT.NONE);
        mainArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        mainArea.setLayout(new GridLayout(1, false));

        /* adds the title group */
        titleCheckBoxes = new ArrayList<Button>(Title.values().length - 1);
        Group titleGroup = createGroup("Title", mainArea);
        titleGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
        titleGroup.setLayout(new GridLayout(Title.values().length - 1, false));
        for (Title title : Title.values()) {
            if (title != Title.UNKNOWN) {
                createTitleCheckBox(title, titleGroup);
            }
        }

        /* adds the name group */
        Group nameGroup = createGroup("Name", mainArea);
        nameGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
        nameGroup.setLayout(new GridLayout(2, false));
        firstNameText = createTextField("First Name:", nameGroup);
        middleNameText = createTextField("Middle Name:", nameGroup);
        lastNameText = createTextField("Last Name:", nameGroup);
        nameSuffixText = createTextField("Name Suffix:", nameGroup);
        nicknameText = createTextField("Nickname:", nameGroup);
        if (searchToEdit != null) {
            setTextFieldValue(firstNameText, searchToEdit.getQuery().getFirstNameList());
            setTextFieldValue(middleNameText, searchToEdit.getQuery().getMiddleNameList());
            setTextFieldValue(lastNameText, searchToEdit.getQuery().getLastNameList());
            setTextFieldValue(nameSuffixText, searchToEdit.getQuery().getNameSuffixList());
            setTextFieldValue(nicknameText, searchToEdit.getQuery().getNicknameList());
        }

        /* adds the gender group */
        genderCheckBoxes = new ArrayList<Button>(Gender.values().length - 1);
        Group genderGroup = createGroup("Gender", mainArea);
        genderGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
        genderGroup.setLayout(new GridLayout(Gender.values().length - 1, false));
        for (Gender gender : Gender.values()) {
            if (gender != Gender.UNKNOWN) {
                createGenderCheckBox(gender, genderGroup);
            }
        }

        /* adds the party group */
        partyCheckBoxes = new ArrayList<Button>(Party.values().length - 1);
        Group partyGroup = createGroup("Party", mainArea);
        partyGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
        partyGroup.setLayout(new GridLayout(Party.values().length - 1, false));
        for (Party party : Party.values()) {
            if (party != Party.UNKNOWN) {
                createPartyCheckBox(party, partyGroup);
            }
        }

        /* adds the active/inactive group */
        inOfficeCheckBoxes = new ArrayList<Button>(2);
        Group inOfficeGroup = createGroup("In Office", mainArea);
        inOfficeGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
        inOfficeGroup.setLayout(new GridLayout(2, false));
        createInOfficeCheckBox("Active", Boolean.TRUE, inOfficeGroup);
        createInOfficeCheckBox("Inactive", Boolean.FALSE, inOfficeGroup);

        /* sets the dialog main area */
        item.setControl(mainArea);
    }

    /**
     * Creates the tab item with the location search controls (state and district).
     * 
     * @param tabFolder
     *            the tab folder.
     */
    private void createLocationTabItem(CTabFolder tabFolder) {
        CTabItem item = new CTabItem(tabFolder, SWT.NONE);
        item.setText("Location");
        item.setImage(SunlightImages.LOCATION.get());

        /* adds the main composite */
        Composite mainArea = new Composite(tabFolder, SWT.NONE);
        mainArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        mainArea.setLayout(new GridLayout(1, false));
        item.setControl(mainArea);

        /* adds the state group */
        Group stateGroup = createGroup("State", mainArea);
        stateGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        stateGroup.setLayout(new GridLayout(2, false));
        stateTableViewer = CheckboxTableViewer.newCheckList(stateGroup, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
        GridData tableGridData = new GridData(SWT.FILL, SWT.FILL, true, true);
        tableGridData.heightHint = ControlUtils.convertHeightInCharsToPixels(stateTableViewer.getTable(), 20);
        stateTableViewer.getTable().setLayoutData(tableGridData);
        stateTableViewer.setLabelProvider(new LabelProvider() {
            @Override
            public String getText(Object element) {
                State state = (State) element;
                return state.toString() + " (" + state.getAbbreviation() + ')';
            }
        });
        stateTableViewer.setContentProvider(new ArrayContentProvider());
        stateTableViewer.setInput(State.values());
        Composite stateButtonArea = new Composite(stateGroup, SWT.NONE);
        stateButtonArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true));
        stateButtonArea.setLayout(new GridLayout(1, true));
        Button selectAllButton = new Button(stateButtonArea, SWT.PUSH);
        selectAllButton.setText("Select All");
        selectAllButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
        selectAllButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                stateTableViewer.setAllChecked(true);
            }
        });
        Button deselectAllButton = new Button(stateButtonArea, SWT.PUSH);
        deselectAllButton.setText("Deselect All");
        deselectAllButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
        deselectAllButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                stateTableViewer.setAllChecked(false);
            }
        });
        if (searchToEdit != null) {
            stateTableViewer.setCheckedElements(searchToEdit.getQuery().getStateList().toArray());
        }

        /* adds the district group */
        Group districtGroup = createGroup("District", mainArea);
        districtGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        districtGroup.setLayout(new GridLayout(2, false));
        districtNumberText = createTextField("District Number(s):", districtGroup);
        if (searchToEdit != null) {
            setTextFieldValue(districtNumberText, searchToEdit.getQuery().getDistrictList());
        }
    }

    /**
     * Creates the tab item with the contact controls (twitter, youtube, etc.).
     * 
     * @param tabFolder
     *            the tab folder.
     */
    private void createContactTabItem(CTabFolder tabFolder) {
        CTabItem item = new CTabItem(tabFolder, SWT.NONE);
        item.setText("Contact");
        item.setImage(SunlightImages.CONTACT.get());

        /* adds the main composite */
        Composite mainArea = new Composite(tabFolder, SWT.NONE);
        mainArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        mainArea.setLayout(new GridLayout(2, false));

        /* adds the search fields */
        Group contactGroup = createGroup("Contact", mainArea);
        contactGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        contactGroup.setLayout(new GridLayout(2, false));
        bioguideText = createTextField("Bioguide ID:", contactGroup);
        voteSmartText = createTextField("Vote Smart ID:", contactGroup);
        fecText = createTextField("FEC ID:", contactGroup);
        govTrackText = createTextField("GovTrack.us ID:", contactGroup);
        crpText = createTextField("CRP ID:", contactGroup);
        eventfulText = createTextField("Eventful.com ID:", contactGroup);
        twitterText = createTextField("Twitter ID:", contactGroup);
        if (searchToEdit != null) {
            setTextFieldValue(bioguideText, searchToEdit.getQuery().getBioguideIdList());
            setTextFieldValue(voteSmartText, searchToEdit.getQuery().getVoteSmartIdList());
            setTextFieldValue(fecText, searchToEdit.getQuery().getFecIdList());
            setTextFieldValue(govTrackText, searchToEdit.getQuery().getGovTrackIdList());
            setTextFieldValue(crpText, searchToEdit.getQuery().getCrpIdList());
            setTextFieldValue(eventfulText, searchToEdit.getQuery().getEventfulIdList());
            setTextFieldValue(twitterText, searchToEdit.getQuery().getTwitterIdList());
        }

        /* sets the dialog main area */
        item.setControl(mainArea);
    }

    /**
     * Creates a group.
     * 
     * @param text
     *            the group's text.
     * @param parent
     *            the parent control.
     * @return the newly created group.
     */
    private Group createGroup(String text, Composite parent) {
        Group group = new Group(parent, SWT.NONE);
        group.setText(text);
        group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        return group;
    }

    /**
     * Creates a checkbox to select a title (senator, representative, etc.).
     * 
     * @param title
     *            the legislator's title.
     * @param parent
     *            the parent control.
     * @return the newly created checkbox.
     */
    private Button createTitleCheckBox(Title title, Composite parent) {
        Button checkBox = createCheckBox(title.toString(), parent);
        checkBox.setData(title);
        titleCheckBoxes.add(checkBox);
        if (searchToEdit != null) {
            checkBox.setSelection(searchToEdit.getQuery().getTitleList().contains(title));
        }
        return checkBox;
    }

    /**
     * Creates a checkbox to select a party.
     * 
     * @param party
     *            the legislator's party.
     * @param parent
     *            the parent control.
     * @return the newly created checkbox.
     */
    private Button createPartyCheckBox(Party party, Composite parent) {
        Button checkBox = createCheckBox(party.toString(), parent);
        checkBox.setData(party);
        partyCheckBoxes.add(checkBox);
        if (searchToEdit != null) {
            checkBox.setSelection(searchToEdit.getQuery().getPartyList().contains(party));
        }
        return checkBox;
    }

    /**
     * Creates a checkbox to select the gender.
     * 
     * @param gender
     *            the legislator's gender.
     * @param parent
     *            the parent control.
     * @return the newly created checkbox.
     */
    private Button createGenderCheckBox(Gender gender, Composite parent) {
        Button checkBox = createCheckBox(gender.toString(), parent);
        checkBox.setData(gender);
        genderCheckBoxes.add(checkBox);
        if (searchToEdit != null) {
            checkBox.setSelection(searchToEdit.getQuery().getGenderList().contains(gender));
        }
        return checkBox;
    }

    /**
     * Creates a checkbox to select the active state.
     * 
     * @param label
     *            the checkbox label.
     * @param value
     *            the boolean value associated with the active state.
     * @param parent
     *            the parent control.
     * @return the newly created checkbox.
     */
    private Button createInOfficeCheckBox(String label, Boolean value, Composite parent) {
        Button checkBox = createCheckBox(label, parent);
        checkBox.setData(value);
        inOfficeCheckBoxes.add(checkBox);
        if (searchToEdit != null) {
            checkBox.setSelection(searchToEdit.getQuery().getInOfficeList().contains(value));
        }
        return checkBox;
    }

    /**
     * Creates a checkbox.
     * 
     * @param text
     *            the checkbox label.
     * @param parent
     *            the parent control.
     * @return the newly created checkbox.
     */
    private Button createCheckBox(String text, Composite parent) {
        Button checkBox = new Button(parent, SWT.CHECK);
        checkBox.setText(text);
        return checkBox;
    }

    /**
     * Creates a text field, composed by a label and a text control.
     * 
     * @param labelText
     *            the label's text.
     * @param parent
     *            the parent control.
     * @return the text control.
     */
    private Text createTextField(String labelText, Composite parent) {
        Label label = new Label(parent, SWT.NONE);
        label.setText(labelText);
        label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
        Text text = new Text(parent, SWT.BORDER);
        text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        return text;
    }

    /**
     * Sets a text field value starting from a list of tokens (quoted and/or unquoted).
     * 
     * @param text   the text field.
     * @param values   the list of tokens to be displayed in the field.
     */
    private void setTextFieldValue(Text text, List<String> values) {
        List<String> tokens = new ArrayList<String>(values.size());
        for (String value : values) {
            boolean quote = false;
            for (int i = 0; i < value.length(); i++) {
                if (Character.isWhitespace(value.charAt(i))) {
                    quote = true;
                    break;
                }
            }
            if (quote) {
                tokens.add("\"" + value + "\"");
            } else {
                tokens.add(value);
            }
        }
        text.setText(StringUtils.join(tokens, " "));
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
     */
    @Override
    protected void createButtonsForButtonBar(Composite parent) {
        createButton(parent, IDialogConstants.OK_ID, "Run", true);
        createButton(parent, IDialogConstants.CANCEL_ID, "Close", false);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
     */
    @Override
    protected void buttonPressed(final int buttonId) {
        if (buttonId == IDialogConstants.OK_ID) {

            /* stores the name of the search */
            searchName = StringUtils.defaultString(searchNameText.getText());

            /* builds and stores the query */
            updateQuery(/* query */);
        }
        super.buttonPressed(buttonId);
    }

    /**
     * Updates the query based on the user's selections.
     */
    private void updateQuery() {
        query = new LegislatorQuery();
        for (Button titleCheckButton : titleCheckBoxes) {
            if (titleCheckButton.getSelection()) {
                query.getTitleList().add((Title) titleCheckButton.getData());
            }
        }
        if (!StringUtils.isBlank(firstNameText.getText())) {
            for (String token : SearchUtils.getTokens(firstNameText.getText())) {
                query.getFirstNameList().add(token);
            }
        }
        if (!StringUtils.isBlank(middleNameText.getText())) {
            for (String token : SearchUtils.getTokens(middleNameText.getText())) {
                query.getMiddleNameList().add(token);
            }
        }
        if (!StringUtils.isBlank(lastNameText.getText())) {
            for (String token : SearchUtils.getTokens(lastNameText.getText())) {
                query.getLastNameList().add(token);
            }
        }
        if (!StringUtils.isBlank(nameSuffixText.getText())) {
            for (String token : SearchUtils.getTokens(nameSuffixText.getText())) {
                query.getNameSuffixList().add(token);
            }
        }
        if (!StringUtils.isBlank(nicknameText.getText())) {
            for (String token : SearchUtils.getTokens(nicknameText.getText())) {
                query.getNicknameList().add(token);
            }
        }
        for (Button partyCheckButton : partyCheckBoxes) {
            if (partyCheckButton.getSelection()) {
                query.getPartyList().add((Party) partyCheckButton.getData());
            }
        }
        for (Button genderCheckButton : genderCheckBoxes) {
            if (genderCheckButton.getSelection()) {
                query.getGenderList().add((Gender) genderCheckButton.getData());
            }
        }
        if (inOfficeCheckBoxes.get(0).getSelection()) {
            query.getInOfficeList().add(true);
        }
        if (inOfficeCheckBoxes.get(1).getSelection()) {
            query.getInOfficeList().add(false);
        }
        for (Object object : stateTableViewer.getCheckedElements()) {
            State state = (State) object;
            query.getStateList().add(state);
        }
        if (!StringUtils.isBlank(districtNumberText.getText())) {
            for (String token : StringUtils.split(districtNumberText.getText())) {
                try {
                    int number = Integer.parseInt(token);
                    query.getDistrictList().add(Integer.toString(number));
                } catch (NumberFormatException e) {
                }
            }
        }
        if (!StringUtils.isBlank(bioguideText.getText())) {
            for (String token : SearchUtils.getTokens(bioguideText.getText())) {
                query.getBioguideIdList().add(token);
            }
        }
        if (!StringUtils.isBlank(voteSmartText.getText())) {
            for (String token : SearchUtils.getTokens(voteSmartText.getText())) {
                query.getVoteSmartIdList().add(token);
            }
        }
        if (!StringUtils.isBlank(fecText.getText())) {
            for (String token : SearchUtils.getTokens(fecText.getText())) {
                query.getFecIdList().add(token);
            }
        }
        if (!StringUtils.isBlank(govTrackText.getText())) {
            for (String token : SearchUtils.getTokens(govTrackText.getText())) {
                query.getGovTrackIdList().add(token);
            }
        }
        if (!StringUtils.isBlank(crpText.getText())) {
            for (String token : SearchUtils.getTokens(crpText.getText())) {
                query.getCrpIdList().add(token);
            }
        }
        if (!StringUtils.isBlank(eventfulText.getText())) {
            for (String token : SearchUtils.getTokens(eventfulText.getText())) {
                query.getEventfulIdList().add(token);
            }
        }
        if (!StringUtils.isBlank(twitterText.getText())) {
            for (String token : SearchUtils.getTokens(twitterText.getText())) {
                query.getTwitterIdList().add(token);
            }
        }
        if (searchToEdit != null) {
            try {
                CongressExplorerPlugin.getSearchManager().updateSearchQuery(searchToEdit, query);
            } catch (SunlightException e) {
                ErrorDialog.openError(getShell(), "Error", e.getMessage(), new Status(IStatus.ERROR, CongressExplorerPlugin.PLUGIN_ID,
                        e.getMessage(), e));
            }
        }
    }

    /**
     * Returns the legislator query.
     * 
     * @return the legislator query.
     */
    public LegislatorQuery getLegislatorQuery() {
        return query;
    }

    /**
     * Returns the name of the search.
     * 
     * @return the name of the search.
     */
    public String getSearchName() {
        return searchName;
    }

}
